package org.mongodb;

import java.io.File;
import java.net.UnknownHostException;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

public class Mongofs {

	private static final String USER = "rechard";
	private static final String PASS = "root";
	private static Mongo m;
	private static DB db;
	private static String Host = "localhost";
	private static String dbName = "test";

	public Mongofs(String Host, String dbName) {
		Mongofs.Host = Host;
		Mongofs.dbName = dbName;
		init();
	}

	public Mongofs() {
		init();
	}

	static void init() {
		try {
			m = new Mongo(Host);
			db = m.getDB(dbName);
			db.requestStart();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}
	}

	public static void quicksave(byte[] data, String fileName) {
		init();
		try {
			storeFile("fs", data, fileName);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close();
	}

	/**
	 * 存储文件到mongo
	 * 
	 * @param db
	 * @param bucket
	 * @param file
	 * @param fn
	 * @throws Exception
	 */
	public void storeFile(String bucket, String file, String fn)
			throws Exception {
		File files = new File(file);
		// 创建�?��GridFS实例
		GridFS gfs = new GridFS(db, bucket);
		GridFSInputFile gfsInput = gfs.createFile(files);
		// 指定�?��GridFS实体的名�?
		gfsInput.setFilename(fn);
		gfsInput.save();
	}

	/**
	 * 存储文件到mongo
	 * 
	 * @param db
	 * @param bucket
	 * @param data
	 * @param fn
	 * @throws Exception
	 */
	public static void storeFile(String bucket, byte[] data, String fn)
			throws Exception {
		// 创建�?��GridFS实例
		GridFS gfs = new GridFS(db, bucket);
		GridFSInputFile gfsInput = gfs.createFile(data);
		// 指定�?��GridFS实体的名�?
		gfsInput.setFilename(fn);
		gfsInput.save();
	}

	/**
	 * 查找单个文件并保�?
	 * 
	 * @param db
	 * @param bucket
	 * @param fn
	 * @param saveTo
	 * @throws Exception
	 */
	public void findSingleFileAndSaveAs(String bucket, String fn, String saveTo)
			throws Exception {
		GridFS gfs = new GridFS(db, bucket);
		GridFSDBFile dbFile = gfs.findOne(fn);
		// System.out.println(dbFile);
		if (dbFile != null)
			dbFile.writeTo(saveTo);
		// System.out.println("file size:" + dbFile.writeTo(saveTo));
	}

	/**
	 * 从mongodb删除文件
	 * 
	 * @param db
	 * @param bucket
	 * @param fn
	 */
	public void remove(String bucket, String fn) {
		GridFS gfs = new GridFS(db, bucket);
		gfs.remove(fn);
	}

	public static void close() {
		db.requestDone();
	}

	public static void main(String[] args) throws Exception {

		String newFilename, bucket, saveTo;
		Mongofs mongofs = new Mongofs();
		String imageUrl = "http://web.qq.com/cgi-bin/get_group_pic?type=0&gid1303895317&uin=3962040174&rip=123.138.238.110&rport=8000&fid=2938177918&pic={3443108D-13C7-E7BF-9182-CDD170E18410}.jpg&vfwebqq=86e1207d0a965a631676f708e47e051702516104ed2cfee4649bcd6e9cdf223eb26d693bf072e8cdt=1358524027/";
		bucket = "fs";
		newFilename = "mm";
		saveTo = "img";
		byte[] data = ImageReaderUtil.getBytesByURL(imageUrl);
		mongofs.storeFile(bucket, data, saveTo);
		mongofs.findSingleFileAndSaveAs(bucket, newFilename, saveTo);
		mongofs.close();

		// Mongo m = new Mongo("localhost");
		// DB db = m.getDB("test");
		// // boolean auth = db.authenticate(USER, PASS.toCharArray());
		// boolean auth = true;
		// if (auth) {
		// // DBCollection coll = db.getCollection("testColl");
		// db.requestStart();
		//
		// String file, newFilename, bucket, saveTo;
		// long startTime = System.currentTimeMillis();
		// for (int i = 0; i < 10000; i++) {
		// bucket = "fs";
		// newFilename = "mm";
		// file = "Q:/008.jpg";
		// saveTo = "img" + i;
		//
		// storeFile(bucket, file, newFilename);
		//
		// findSingleFileAndSaveAs(db, bucket, newFilename, saveTo);
		//
		// // remove(db, bucket, newFilename);
		// }
		// System.out.println(System.currentTimeMillis()-startTime);
		// db.requestDone();
		// }
	}

}